/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Airplane;
import edu.chl.da.ejb.core.Seat;
import edu.chl.da.ejb.ctrl.IManageAirplane;
import edu.chl.da.ejb.ctrl.IManageSeat;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

/**
 * Bean handling the add airplane event.
 * @author Kristofer
 */
@ManagedBean(name = "AddAirplaneBean")
@RequestScoped
public class AddAirplaneBean {

    private String model;
    private String errorMsg;
    private boolean success;


    @EJB
    IManageAirplane ima;

    /** Creates a new instance of AddAirportBean */
    public AddAirplaneBean() {
        errorMsg = null;
    }

    public void addAirplane(ActionEvent e)
            throws AbortProcessingException {
        if (model == null || ("").equals(model)) {
            this.success = false;
            this.errorMsg = "Alla falt maste vara ifyllda.";
        } else {
            this.success = true;
            Airplane a = ima.createAirplane(model);
            ima.createSeatsForAirplane(a);
        }
    }

    public String navigate() {
        if (success) {
            return "/admin/manageAirplanes.jsf";
        } else {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(errorMsg));
            return null; //Same page;
        }
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }

}
